package com.ccx.coachsite.ui.custom;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.ExpertExInfo;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.bus.levelInfo;
import com.ccx.coachsite.ui.login.LoginHelper;
import com.ccx.coachsite.ui.manage.ManageKey;
import com.ccx.coachsite.ui.own.OwerBaseView;
import com.ccx.coachsite.ui.own.OwerViewKey;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;

public class MyCustomView extends OwerBaseView {
	public static final String TAG = "MyCustomView";

	private View mBtnCustom;
	private View mBtnGroup;
	private View mBtnMessage;
	private View mBtnInvite;
	private View mBtnAddCustom;
	private TextView mMessageCount;

	public MyCustomView(Context context, CustomMainView superView) {
		super(context, superView);
		isRootView = true;

		mBtnCustom = findViewById(R.id.cuttom_button_custom);
		mBtnCustom.setOnClickListener(new OnCustomListener());

		mBtnGroup = findViewById(R.id.cuttom_button_group);
		mBtnGroup.setOnClickListener(new OnGroupListener());

		mBtnInvite = findViewById(R.id.cuttom_button_invite);
		mBtnInvite.setOnClickListener(new OnInviteCustomListener());

		mBtnMessage = findViewById(R.id.cuttom_button_message);
		mBtnMessage.setOnClickListener(new OnCustomMessageListener());
		
		mBtnAddCustom = findViewById(R.id.cuttom_button_add_custom);
		mBtnAddCustom.setOnClickListener(new OnAddCustomListener());


		mMessageCount = (TextView) findViewById(R.id.txt_custom_new_message_count);
		String countStr = SharedPreferencesUtil.getValue(context,
				SharedKey.Custom_Message_Count, "");
		if (!StringUtil.isEmpty(countStr) && !countStr.equals("0")) {
			mMessageCount.setText(countStr);
			mMessageCount.setVisibility(View.VISIBLE);
		} else {
			// mMessageCount.setVisibility(View.GONE);
		}

		initExInfo();
	}
	
	public void onLoadSuccess()
	{
		initExInfo();
	}

	public static int getScreenWidth(Context context) {
		return context.getResources().getDisplayMetrics().widthPixels;
	}

	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	public void initExInfo() {
		ExpertExInfo einfo = ExpertExInfo.getExpertExInfo(mContext);
		if (einfo != null) {
			TextView tv = (TextView) findViewById(R.id.cuttom_expert_place_info);
			tv.setText(einfo.getPlaceName());
			if(einfo.getPlaceName()!= null && einfo.getPlaceName().equals("null"))
				tv.setText("");
			
			tv = (TextView) findViewById(R.id.cuttom_expert_level_info);
			tv.setText(" " + einfo.getLevelName() + " ");

			tv = (TextView) findViewById(R.id.cuttom_expert_num_info);
			tv.setText(einfo.getClaimedNums() + "");

			if (einfo.getLevelCount() > 0) {
				int chaim = einfo.getClaimedNums();
				RelativeLayout labelPanel = (RelativeLayout) findViewById(R.id.custom_level_number_panel);
				ImageView imgFlag = (ImageView) findViewById(R.id.custom_level_flag_img);
				int sw = getScreenWidth(mContext);
				int panelW = sw - dip2px(mContext, 36);
				float totalCount = einfo.getLevelList()
						.get(einfo.getLevelList().size() - 1).getLevelNum();

				if (chaim > 0) {
					ViewGroup.LayoutParams lpv = imgFlag.getLayoutParams();
					if (lpv != null && lpv instanceof RelativeLayout.LayoutParams) {
						RelativeLayout.LayoutParams lpi = (RelativeLayout.LayoutParams) lpv;
						int leftm = (int) (chaim * panelW / totalCount);
						leftm -= 2;
						if(leftm < 0)
							leftm = 0;
						lpi.leftMargin = leftm;
					}

					imgFlag.setLayoutParams(lpv);
				}
				float tempF = 0.0f;
				for (int i = 0; i < einfo.getLevelList().size(); i++) {
					levelInfo linfo = einfo.getLevelList().get(i);

					TextView tvTitle = (TextView)labelPanel.findViewById(100000+linfo.getLevelNum());
					if(tvTitle != null)
						continue;
					
					tvTitle = new TextView(mContext);
					
					tvTitle.setText(linfo.getLevelNum() + "");
					tvTitle.setTextColor(Color.WHITE);
					tvTitle.setGravity(Gravity.RIGHT);
					RelativeLayout.LayoutParams lpq = new RelativeLayout.LayoutParams(
							RelativeLayout.LayoutParams.FILL_PARENT,
							RelativeLayout.LayoutParams.WRAP_CONTENT);
					//lpq.weight = totalCount - (linfo.getLevelNum() - tempF);
					tempF = (float) (1.0 * linfo.getLevelNum());
					
					// lp.weight = 1;
					//lpq.gravity = Gravity.RIGHT;
					
					lpq.rightMargin = (int) ((totalCount - tempF) * panelW / totalCount);
					tvTitle.setLayoutParams(lpq);
					tvTitle.setId(100000+linfo.getLevelNum());
					labelPanel.addView(tvTitle);
					
				}
			}
		}
	}

	public void customInfoShow() {
		LayoutInflater mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View smile_view = mInflater.inflate(R.layout.popwindow_view, null);

		LinearLayout layMenu = (LinearLayout) smile_view
				.findViewById(R.id.pop_windows_layout);
		layMenu.setFocusable(true);

		ExpertExInfo einfo = ExpertExInfo.getExpertExInfo(mContext);
		if (einfo != null && einfo.getLevelCount() > 0) {
			LinearLayout panel = (LinearLayout) smile_view
					.findViewById(R.id.custom_info_panel);
			for (int i = 0; i < einfo.getLevelList().size(); i++) {
				levelInfo linfo = einfo.getLevelList().get(i);
				String textTitle = linfo.getLevelName() + "可认领用户上限";
				TextView tvTitle = new TextView(mContext);
				tvTitle.setText(textTitle);
				tvTitle.setTextColor(Color.BLACK);
				tvTitle.setTextSize(16);
				panel.addView(tvTitle);

				LinearLayout lvSub = new LinearLayout(mContext);
				lvSub.setOrientation(LinearLayout.HORIZONTAL);

				TextView tvNum = new TextView(mContext);
				tvNum.setText(linfo.getLevelNum() + "");
				tvNum.setTextColor(Color.RED);
				tvNum.setTextSize(16);
				lvSub.addView(tvNum);

				TextView tvDesc = new TextView(mContext);
				tvDesc.setText("  名客户");

				tvDesc.setTextColor(Color.BLACK);
				tvDesc.setTextSize(16);
				lvSub.addView(tvDesc);

				panel.addView(lvSub);
			}
		}

		final AlertDialog dialog = new AlertDialog.Builder(mContext).create();
		dialog.show();
		dialog.getWindow().setContentView(smile_view);

		smile_view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (dialog != null) {
					dialog.dismiss();
				}
			}

		});

		/*
		 * final PopupWindow popupWindow = new PopupWindow(smile_view,
		 * LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);
		 * popupWindow.showAtLocation(mSuperView, Gravity.CENTER |
		 * Gravity.CENTER, 0, 0);
		 * popupWindow.setAnimationStyle(R.anim.popwindow_animation); //
		 * 加上下面两行可以用back键关闭popupwindow，否则必须调用dismiss(); ColorDrawable dw = new
		 * ColorDrawable(-00000); popupWindow.setBackgroundDrawable(dw);
		 * smile_view.setFocusable(true);
		 * 
		 * // popupWindow.setFocusableInTouchMode(true); //为了保险起见加上这句
		 * popupWindow.setBackgroundDrawable(new BitmapDrawable()); //
		 * 响应返回键必须的语句 popupWindow.setFocusable(true);
		 * 
		 * // layMenu.setOnKeyListener(new OnKeyListener() {// 手机键盘上的返回键 //
		 * public boolean onKey(View v, int keyCode, KeyEvent event) { // switch
		 * (keyCode) { // case KeyEvent.KEYCODE_BACK: // if (popupWindow != null
		 * && popupWindow.isShowing()) { // popupWindow.dismiss(); // return
		 * true; // } // break; // } // return false; // } // });
		 * 
		 * popupWindow.update();
		 */
	}

	class OnCustomListener implements OnClickListener {
		@Override
		public void onClick(View v) {

			if (!LoginHelper.requireLogin(mContext))
				return;

			SwitchView(ManageKey.ll_manage_custom, 1);

		}
	}

	class OnGroupListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (!LoginHelper.requireLogin(mContext))
				return;

			SwitchView(ManageKey.ll_manage_group);
		}
	}

	class OnInviteCustomListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (!LoginHelper.requireLogin(mContext))
				return;
			SwitchView(ManageKey.ll_manage_custom, 2);
			// SwitchView(ManageKey.ll_manage_add_custom);
		}
	}
	
	class OnAddCustomListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (!LoginHelper.requireLogin(mContext))
				return;
			SwitchView(ManageKey.ll_manage_add_custom);
			// SwitchView(ManageKey.ll_manage_add_custom);
		}
	}

	class OnCustomMessageListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (!LoginHelper.requireLogin(mContext))
				return;
			SwitchView(ManageKey.ll_manage_custom, 3);
			// SwitchView(ManageKey.ll_manage_add_custom);
		}
	}

}
